package org.boot.easyapi.logger.dao;

import org.boot.easyapi.logger.entity.OperationLoggerDO;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class OperationLoggerRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    private static final String SQL =
            "INSERT INTO easyapi_core_operation_logger " +
                    "(url, method, ip, class_name, method_name, request_params, " +
                    "response_result, execution_time, create_time, operation_user," +
                    "operation_user_json, success, operation_tag) " +
                    "VALUES" +
                    " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    @Async
    public void save(OperationLoggerDO log) {
        jdbcTemplate.update(SQL,
                log.getUrl(),
                log.getMethod(),
                log.getIp(),
                log.getClassName(),
                log.getMethodName(),
                log.getRequestParams(),
                log.getResponseResult(),
                log.getExecutionTime(),
                log.getCreateTime(),
                log.getOperationUser(),
                log.getOperationUserJson(),
                log.getSuccess(),
                log.getOperationTag()
        );
    }
}
